package se.webbzon.boltzmann.hud;

import java.awt.image.ImageObserver;

import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldcamera.WorldCameraPane;
import se.webbzon.oschi01.worldobject.WorldObject;

public class HudCursor extends WorldObject {
	
	// The 2-dimensional depth of the cursor
	private static final int depth = -2;
	
	// The layout of the cursor
	private final CursorLayout layout;
	
	// Whatever the cursor is visible
	private boolean visible;
	
	// The x- and y-position of the cursor
	private int x, y;
	
	// The rotation angle of the cursor
	private double angle, t;
	
	/*============================================================
	Constructor
	============================================================*/
	
	/** Creates a new game inventory cursor. **/
	public HudCursor(CursorLayout layout) {
		this(0,depth,layout);
	}
	
	/** Creates a new game inventory cursor with the provided set
	 * of registered methods. **/
	public HudCursor(int registeredMethods, int depth, CursorLayout layout) {
		super(registeredMethods | WorldObject.onStep | WorldObject.onPostDraw,depth);
		this.layout = layout;
		this.x = 0;
		this.y = 0;
		this.angle = 0;
		this.t = 0;
		this.visible = false;
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the layout of this cursor. **/
	public CursorLayout getLayout() {
		return layout;
	}
	
	/** Sets whatever the cursor is visible. **/
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	/** Returns true if the cursor is visible. **/
	public boolean getVisible() {
		return visible;
	}
	
	/** Sets the position of the cursor. **/
	public void setPosition(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	/** Returns the x-position of the cursor. **/
	public int getX() {
		return x;
	}
	
	/** Returns the y-position of the cursor. **/
	public int getY() {
		return y;
	}
	
	/** Draws the cursor on the given heads-up display. **/
	@Override public void onPostDraw(WorldArea world, WorldCameraPane hud) {
		if (!visible)
			return;
		
		final int xo = (int) (layout.getRadius()*Math.cos(angle));
		final int yo = (int) (layout.getRadius()*Math.sin(angle));
		final ImageObserver o = hud.getImageObserver();
		final int imageWidth = layout.getCursorImageSet().get(0).getWidth(o);
		final int imageHeight = layout.getCursorImageSet().get(0).getHeight(o);
		final int w = imageWidth/2, h = imageHeight/2;
		final int xd = x-w, yd = y-h;
		hud.drawImage(layout.getCursorImageSet().get(0), xd+xo, yd+yo);
		hud.drawImage(layout.getCursorImageSet().get(1), xd-yo, yd+xo);
		hud.drawImage(layout.getCursorImageSet().get(2), xd-xo, yd-yo);
		hud.drawImage(layout.getCursorImageSet().get(3), xd+yo, yd-xo);
	}
	
	@Override public void onStep(WorldArea world) {
		t += Math.PI/90;
		angle = Math.PI*Math.sin(t);
	}

}
